<template>

  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" label-width="70px" :model="searchForm" class="demo-form-inline">
        <el-form-item label="职业">
          <el-select size="small" v-model="searchForm.career_id" placeholder="请选择">
            <el-option label="全部" :value="0"></el-option>
            <el-option v-for="(item, index) in positionList" :key="index" :label="item.title"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="级别">
          <el-select size="small" v-model="searchForm.level_id" placeholder="请选择">
            <el-option label="全部" :value="0"></el-option>
            <el-option v-for="(item, index) in levelList" :key="index" :label="item.title" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向">
          <el-select size="small" v-model="searchForm.direction_id" placeholder="请选择">
            <el-option label="全部" :value="0"></el-option>
            <el-option v-for="(item, index) in directionList" :key="index" :label="item.title"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="试卷名称">
          <el-input size="small" v-model="searchForm.search" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="试卷类型">
          <el-select size="small" v-model="searchForm.category_id" placeholder="所有分类">
            <el-option label="全部" value="0"></el-option>
            <el-option label="随机出题" :value="9"></el-option>
            <el-option label="固定题目" :value="12"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-content">
      <!--添加管理员-->
      <div class="common-level-rail">
        <el-button size="small" type="primary" @click="addClick">新增试卷</el-button>
      </div>

      <!--内容-->
      <div class="product-content">
        <div class="table-wrap">
          <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
            <el-table-column align="center" prop="title" label="试卷名称" min-width="120"></el-table-column>
            <el-table-column align="center" prop="category_id" label="试卷类型" min-width="80">
              <template #default="scope">
                <span>{{ scope.row.category_id == 9 ? '随机出题' : scope.row.category_id == 12 ? '固定题目' : '' }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="total_score" label="总分数" width="120">
              <template #default="scope">
                <span>{{ scope.row.total_score }}分</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="passing_mark" label="合格分数" min-width="80">
              <template #default="scope">
                <span>{{ scope.row.passing_mark }}分</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="exam_time" label="考试时长" min-width="80">
              <template #default="scope">
                <span>{{ scope.row.exam_time }}分钟</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="remark" label="单选" min-width="100">
              <template #default="scope">
                <span>{{ scope.row.single_number }}题/{{ scope.row.single_score }}分</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="remark" label="多选" min-width="100">
              <template #default="scope">
                <span>{{ scope.row.many_number }}题/{{ scope.row.many_score }}分</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="remark" label="判断" min-width="100">
              <template #default="scope">
                <span>{{ scope.row.judge_number }}题/{{ scope.row.judge_score }}分</span>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="career_title" label="职业" min-width="120"></el-table-column>
            <el-table-column align="center" prop="level_title" label="级别" min-width="120"></el-table-column>
            <el-table-column align="center" prop="direction_title" label="方向" min-width="120"></el-table-column>
            <el-table-column align="center" fixed="right" label="操作" min-width="120">
              <template #default="scope">
                <el-button @click="editClick(scope.row)" type="text" size="small">编辑</el-button>
                <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!--分页-->
        <div class="pagination">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
            :total="totalDataNumber"></el-pagination>
        </div>
      </div>
    </div>


    <Add v-if="open_add" ref="open_add" @close="closeAdd"></Add>

    <Edit :open="open_edit" :roleList="roleList" :shop_user_id="curModel.shop_user_id" @close="closeEdit"></Edit>
    <el-dialog title="删除试卷" style="width: 251px;" modal-class="dialog-add" v-model="dialogVisible"
      @close="dialogVisible = false" :close-on-click-modal="false" :close-on-press-escape="false">
      是否确定要删除试卷？
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="delExamination()" :loading="deleteloading">是</el-button>
          <el-button @click="dialogVisible = false">否</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import PositionApi from '@/api/position.js';
import ExaminationApi from '@/api/examination.js';

import Add from './dialog/Add.vue';
import Edit from './dialog/Edit.vue';
export default {
  components: {
    Add,
    Edit
  },
  inject: ['reload'],
  data() {
    return {
      positionList: [], //全部职业
      levelList: [],
      directionList: [],
      dialogVisible: false,
      deleteloading: false,
      paper_id: '',

      /*是否加载完成*/
      loading: true,
      /*角色是否加载完成*/
      role_loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        user: '',
        region: ''
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      curModel: {},
      /*角色列表*/
      roleList: [],
      searchForm: {
        name: '',
        phone: ''
      }
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
    // 获取职位列表
    this.getPositionAll()
    this.getLevelAll()
    this.getDirectionAll()
  },
  methods: {
    // 确认删除
    delExamination() {
      let self = this;
      self.deleteloading = true;
      ExaminationApi.delExamination(
        {
          paper_id: self.paper_id
        },
        true
      )
        .then(data => {
          self.deleteloading = false;
          if (data.code == 1) {
            ElMessage({
              message: '删除成功',
              type: 'success'
            })
            self.getTableList();
            this.dialogVisible = false;
          } else {
            self.deleteloading = false;
          }
        }).catch(error => {
          self.deleteloading = false;
        })
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = {
        ...self.searchForm
      };
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      ExaminationApi.getExaminationList(Params, true)
        .then(data => {
          self.loading = false;
          self.role_loading = false;
          self.roleList = data.data.roleList;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
        })
        .catch(error => { });
    },

    /*打开添加*/
    addClick() {
      this.open_add = true;
    },

    /*关闭添加*/
    closeAdd(e) {
      this.open_add = false;
      if (e && e.type == 'success') {
        this.getTableList();
      }
    },

    /*打开编辑*/
    editClick(row) {
      this.open_add = true;
      this.$nextTick(() => {
        this.$refs.open_add.setTitle(row)
      })
    },

    /*关闭添加*/
    closeEdit(e) {
      this.open_edit = false;
      if (e && e.type == 'success') {
        this.getTableList();
      }
    },

    /*删除*/
    deleteClick(row) {
      this.paper_id = row.paper_id;
      this.dialogVisible = true;
    },
    getPositionAll() {
      PositionApi.getNestedData({}, true)
        .then(res => {
          this.positionList = res.data;
        })
        .catch(error => { });
    },
    getLevelAll() {
      PositionApi.getCareerLevel({}, true)
        .then(data => {
          this.levelList = data.data;
        })
        .catch(error => { });
    },
    getDirectionAll() {
      PositionApi.getCareerDirection({}, true)
        .then(data => {
          this.directionList = data.data;
        })
        .catch(error => { });
    },
  }
};
</script>

<style></style>
